<template>
  <s-layout id="layout" :title="'工资条'" navbar="normal" leftIconColor="#333" :autoBack="false" :bgColor="state.bgColor"
    :tabbarshow='false' :titleStyle="{ color: '#333', fontSize: '32rpx' }">
    <view class="call_on list_box" :style="{ paddingTop: (safeAreaInsets?.top + 44) + 'px' }">

      <!-- <view class="screen">
        <view class="left">
          <view class="image"><image :src="`${baseUrl}/uploads/uniapp_image/staff/gongzitiao_icon_time@2.png`" mode="scaleToFill"/></view>
          <view>筛选月份</view>
        </view>
        <view class="right">
          <view class="image">
            <image :src="`${baseUrl}/uploads/uniapp_image/staff/class_icon01.png`" mode="scaleToFill"/>
          </view>
          <view class="text"><text @tap="start_date">{{ start ? start : '开始时间' }}</text> - <text @tap="over_date">{{ over ? over : '结束时间' }}</text></view>
        </view>
      </view> -->
      <!-- - 104rpx -->
      <scroll-view scroll-y :style="{ height: `calc(100vh - ${safeAreaInsets?.top}px - 44px - 40rpx)` }"
        class="comprehensive" v-if="salary_slipList.length" @scrolltolower='listScrolltolower' show-scrollbar="true"
        :refresher-enabled="isOpenRefresh" :refresher-triggered="triggered" :refresher-threshold="100"
        refresher-background="white" @refresherpulling="onPulling" @refresherrefresh="onRefresh"
        @refresherrestore="onRestore" @refresherabort="onAbort" @scroll="onScroll">
        <!-- <view class="searchbox">
          <s-search-keyword :placeholder="'搜索员工名字'" @searchKeyWord="searchKeyWord"></s-search-keyword>
        </view> -->
        <view class="item_wrap">
          <view class="item" v-for="(item, index) in salary_slipList" :key="index">
            <view class="year">{{ item.year }} 年</view>
            <view class="flex-wrap">
              <view class="flex" v-for="(c, v) of item.list" :key="v">
                <image class="flex_img" v-if="sheep.$url.cdn('/uploads/uniapp_image/staff/shenpi_icon10.png')"
                  :src="sheep.$url.cdn('/uploads/uniapp_image/staff/shenpi_icon10.png')" mode="scaleToFill" />
                <view class="item_left">
                  <view class="item_left_box">
                    <view class="title">{{ c.date.split("-")[1] }}月薪资</view>
                  </view>
                  <view class="item_left_box">
                    <view class="name">
                      <view class="name_title">发放时间</view>
                      <text>{{ c.date }}</text>
                      <!-- <up-text mode="date" :text="c.date" color="#666666" size="26rpx"></up-text> -->
                    </view>
                  </view>
                  <view class="item_left_box">
                    <view class="name">
                      <view class="name_title">实发薪资</view> <up-text mode="price" :text="c.real_salary" color="#666666"
                        size="26rpx"></up-text>
                    </view>
                  </view>
                </view>
                <view class="item_right" @tap="skipDetail(c.id, c.date)">
                  <text class="name">查看详情</text>
                  <u-icon size="10" name="arrow-right" color="#0038F7"></u-icon>
                </view>
              </view>
            </view>

          </view>

        </view>
        <!-- 正在加载组件 -->
        <view>
          <s-loadmore-gather :current_page="current_page" :last_page="last_page" ref="loadmoreRef"></s-loadmore-gather>
        </view>

      </scroll-view>

      <u-empty v-else mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png" text="暂无内容"
        marginTop="50%"></u-empty>
    </view>
  </s-layout>
  <up-datetime-picker v-model="start_time" :closeOnClickOverlay="true" :show="start_show" mode="year-month"
    :title="'请选择开始日期'" @confirm="start_confirm" @cancel="start_cancel" @close="start_close"
    :minDate="-2209017943000"></up-datetime-picker>
  <up-datetime-picker v-model="over_time" :closeOnClickOverlay="true" :show="over_show" mode="year-month"
    :title="'请选择结束日期'" @confirm="over_confirm" @cancel="over_cancel" @close="over_close"
    :minDate="-2209017943000"></up-datetime-picker>
</template>

<script setup>
import { baseUrl } from '@/sheep/config';
import sheep from '@/sheep';
import { onLoad, onShow } from "@dcloudio/uni-app";
import { ref, reactive } from 'vue';
import { formatDate } from '@/utils/fn.js';
const { safeAreaInsets } = uni.getSystemInfoSync()
const state = ref({
  bgColor: '#fff',

})

onLoad((options) => {
  getSalary_slipList()
})



// 封装获取工资条列表
const date_range = ref()
const page = ref(1)
const current_page = ref()
const last_page = ref()
const salary_slipList = ref([])
const getSalary_slipList = async () => {
  let params = {
    // filter: JSON.stringify({
    //   date: `${start.value},${over.value}`,
    // }),
    // op: JSON.stringify({
    //   date: 'in',
    // }),
    page: page.value
  }
  const res = await sheep.$api.salary_sheet.salary_slipIndexAPI(params)
  // 判断合并
  if (page.value > 1) {
    if (salary_slipList.value[salary_slipList.value.length - 1].year == res.data.rows[0].year) {
      res.data.rows.forEach(row => {
        const year = row.year;
        const index = salary_slipList.value.findIndex(item => item.year === year);
        if (index !== -1) {
          salary_slipList.value[index].list.push(...row.list);
        }
      });
    }
    salary_slipList.value = [...salary_slipList.value, ...res.data.rows.slice(1)]
  } else {
    salary_slipList.value = [...salary_slipList.value, ...res?.data.rows]
  }
  current_page.value = res.data.current_page
  last_page.value = res.data.last_page
}

// 滚动触底事件 + 分页处理
const loadmoreRef = ref()
const listScrolltolower = () => {
  loadmoreRef.value.PopupScrolltolower()
  if (current_page.value < last_page.value) {
    page.value += 1;
  } else {
    return;
  }
  getSalary_slipList()
}

// 跳转详情页
const skipDetail = (id, date) => {
  sheep.$router.go('/subpackage/staff/user/salary_slip/salary_slip_detail', {
    ids: id,
    date
  })
}
// 页面下拉刷新
const freshing = ref(false)
const triggered = ref(false)
const isOpenRefresh = ref(true)  // 是否开启下拉
// 自定义下拉刷新控件被下拉
const onPulling = (e) => {
  if (e.detail.deltaY < 0) return  // 防止上滑页面也触发下拉
  triggered.value = true;
}
// 自定义下拉刷新被触发
const onRefresh = () => {
  if (freshing.value) return;
  freshing.value = true;
  setTimeout(() => {
    page.value = 1;
    salary_slipList.value = []
    triggered.value = false;
    freshing.value = false;
    getSalary_slipList();
  }, 500);
}
// 自定义下拉刷新被复位
const onRestore = () => {
  triggered.value = 'restore'; // 需要重置
  // console.log("onRestore");
}
// 自定义下拉刷新被中止
const onAbort = () => {
  // console.log("onAbort");
}
</script>

<style lang="scss" scoped>
// 滚动
.comprehensive {
  width: calc(100% - 40rpx);
  background-color: rgba(0, 0, 0, 0);
  margin: 0 20rpx;
  // height: 80vh;
}

.item_wrap {
  margin-top: 0 !important;

  .item {
    background-color: transparent !important;

    .item_right {
      margin-top: 80rpx !important;
    }

    .year {
      height: 90rpx;
      line-height: 90rpx;
      margin-left: 20rpx;
      color: $uni-text-color;
      font-weight: $uni-font-weight;
      font-size: $uni-font-size-big;
    }

    .flex-wrap {
      box-shadow: 0 4rpx 8rpx 0 rgba(0, 0, 0, 0.05);
      border-radius: 16rpx;
      background-color: $uni-bg-color;
    }

    .flex {
      padding-bottom: 10rpx;
      border-bottom: 1rpx solid #efefef;

      &:last-child {
        border-bottom: none;
      }
    }

    .item_left_box {
      .title {
        height: auto;
        margin-bottom: 0rpx;
      }

      .name {
        .name_title {
          margin-right: 20rpx;
        }

        display: flex;
      }

      margin-bottom: 10rpx;
    }
  }
}

.searchbox {
  margin: 30rpx auto 0;
}

// 筛选月份
.screen {
  height: 104rpx;
  background-color: #ffffff;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .left {
    font-family: PingFangSC;
    color: $uni-text-color;
    font-weight: $uni-font-weight;
    font-size: $uni-font-size-base;
    font-style: normal;
    text-decoration: none;
    margin-left: 30rpx;
    display: flex;
    align-items: center;

    .image {
      width: 28rpx;
      height: 28rpx;
      margin-right: 20rpx;

      image {
        width: 100%;
        height: 100%;
        display: flex;
        align-items: center;
      }
    }
  }

  .right {
    // width: 236rpx;
    height: 64rpx;
    border-radius: 16px;
    background-color: #FFFFFF;
    box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.05);
    display: flex;
    align-items: center;
    margin-right: 32rpx;

    .image {
      width: 28rpx;
      height: 28rpx;
      margin-left: 20rpx;
      margin-right: 22rpx;

      image {
        display: flex;
        align-items: center;
        width: 100%;
        height: 100%;
      }
    }

    .text {
      font-family: PingFangSC;
      color: $uni-color-primary;
      font-weight: $uni-font-weight;
     font-size:$uni-font-size-26;
      font-style: normal;
      text-decoration: none;
      margin-right: 22rpx;
    }
  }
}

@import '@/subpackage/staff/workbench/compentns/publiclist.scss';
</style>